/*************************************************************************
 * @file: mvse.cpp
 * @date: 01/04/2012
 * TOPOL v1_0 - (c)copyright 2012 TEC/UFF
 *************************************************************************
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 ************************************************************************/

// includes
//----------
#include "mvse.h"
#include <iostream>
#include "kvje.h"
#include "../hedmodel.h"
#include "../entity/solid.h"
#include "../entity/face.h"
#include "../entity/loop.h"
#include "../entity/vertex.h"
#include "../entity/halfedge.h"
#include "../entity/edge.h"
#include "../entity/ipoint.h"



///////////////////////////////////////////////////
///  Constructor and Destructor Implementation  ///
///////////////////////////////////////////////////

//-------------------------------------------------------------------------
MVSE::MVSE(HEDModel& _model, Edge* _edge, int _v, double _x, double _y, double _z )
: m_edge( _edge )
, m_v( _v )
, m_x( _x )
, m_y( _y )
, m_z( _z )
, m_newVertex( NULL )
, m_model( &_model )
{

}
//-------------------------------------------------------------------------



///////////////////////////////////////
///  PUBLIC Methods Implementation  ///
///////////////////////////////////////

//-------------------------------------------------------------------------
void
MVSE::execute()
{
  std::cout << "\nMVSE::execute() \n";

  if( m_edge == NULL )
  {
    m_edge = m_model->getEdge(m_x,m_y,m_z,1e-5);
  }

  HalfEdge* he1 = m_edge->getHe1();
  HalfEdge* he2 = m_edge->getHe2();
  Loop* loop = he1->getLoop();
  Face* face = loop->getFace();
  Solid* solid = face->getSolid();
  Edge* newedge = new Edge(solid);
  IPoint* point = new IPoint(m_x,m_y,m_z);
  Vertex* newvertex = new Vertex(point,solid);
  newvertex->setVertexno(m_v);
  newedge->setHe2( he2 );
  he2->setEdg( newedge );
  newedge->addhe(newvertex, he1->getNxt(), PLUS); 
  m_edge->addhe(newvertex, he2->getNxt(), MINUS);
  newvertex->setVedge( newedge->getHe1() );

  m_newVertex = newvertex;
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void
MVSE::unExecute()
{
  std::cout << "\nMVSE::unExecute() TESTING!\n";

  KVJE kvje( m_newVertex );
  kvje.execute();

  m_newVertex = NULL;
  m_edge = NULL;

}
//-------------------------------------------------------------------------







